package ph.com.gs3.formalistics.model.facades;

import org.json.JSONException;

import ph.com.gs3.formalistics.model.ldao.DocumentsLDAO;
import ph.com.gs3.formalistics.model.ldao.OutgoingActionsLDAO;
import ph.com.gs3.formalistics.model.ldao.UsersLDAO;
import ph.com.gs3.formalistics.model.vo.business.User;
import ph.com.gs3.formalistics.model.vo.business.document.Document;
import ph.com.gs3.formalistics.model.vo.business.document.OutgoingAction;
import android.content.Context;

public class OutgoingActionDataReader {

	public static final String TAG = OutgoingActionDataReader.class.getSimpleName();

	private OutgoingActionsLDAO outgoingActionsLDAO;
	private UsersLDAO usersLDAO;
	private DocumentsLDAO documentsLDAO;

	public OutgoingActionDataReader(Context context) {
		outgoingActionsLDAO = new OutgoingActionsLDAO(context);
		usersLDAO = new UsersLDAO(context);
		documentsLDAO = new DocumentsLDAO(context);
	}

	public OutgoingAction getOutgoingActionByDocumentId(int documentId) {

		OutgoingAction outgoingAction = null;

		// Get outgoing action
		try {
			outgoingActionsLDAO.open();
			outgoingAction = outgoingActionsLDAO.getOutgoingActionByDocumentId(documentId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			outgoingActionsLDAO.close();
		}

		outgoingAction = assignSubData(outgoingAction);

		return outgoingAction;

	}

	public OutgoingAction getOutgoingAction(int id) {

		OutgoingAction outgoingAction = null;

		// Get outgoing action
		try {
			outgoingActionsLDAO.open();
			outgoingAction = outgoingActionsLDAO.getOutgoingAction(id);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			outgoingActionsLDAO.close();
		}

		outgoingAction = assignSubData(outgoingAction);

		return outgoingAction;
	}

	protected OutgoingAction assignSubData(OutgoingAction outgoingAction) {

		// Get the issuer of the outgoing action
		if (outgoingAction != null) {
			try {
				usersLDAO.open();
				User user = usersLDAO.getUserWithId(outgoingAction.getIssuedByUser().getId());
				outgoingAction.setIssuedByUser(user);
			} finally {
				usersLDAO.close();
			}
		}

		// Get the document of the outgoing action if existing
		if (outgoingAction != null) {

			if (outgoingAction.getDocument().getId() != 0) {
				try {
					documentsLDAO.open();
					int documentId = outgoingAction.getDocument().getId();
					Document document = documentsLDAO.getDocument(documentId);
					outgoingAction.setDocument(document);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					documentsLDAO.close();
				}
			} else {
				outgoingAction.setDocument(null);
			}

		}

		return outgoingAction;

	}	

	public boolean hasNewChildOutgoingActions() {

		boolean hasNewChildOutgoingActions = false;

		try {
			outgoingActionsLDAO.open();
			hasNewChildOutgoingActions = outgoingActionsLDAO.hasNewChildOutgoingActions();
		} finally {
			outgoingActionsLDAO.close();
		}

		return hasNewChildOutgoingActions;

	}
	
}
